package hr.tvz.lab.tom.malwaredb.list;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import hr.tvz.lab.tom.malwaredb.R;
import hr.tvz.lab.tom.malwaredb.entity.MalwareObject;
import hr.tvz.lab.tom.malwaredb.info.InfoActivity;

/**
 * RecyclerView Adapter for MalwareObject list in ListActivity.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    List<MalwareObject> malwareObjectList;

    public RecyclerViewAdapter(List<MalwareObject> malwareObjectList) {
        this.malwareObjectList = malwareObjectList;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        MalwareObject malwareObject;
        TextView name;
        TextView alt;
        TextView alt2;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.textName);
            alt = (TextView) itemView.findViewById(R.id.textAlt);
            alt2 = (TextView) itemView.findViewById(R.id.textAlt2);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Intent infoActivity = new Intent(v.getContext(), InfoActivity.class);
            infoActivity.putExtra("malware", malwareObject);
            // The only way to wait for results. Not practical.
            ((Activity)v.getContext()).startActivityForResult(infoActivity, 1);
        }
    }

    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
        holder.malwareObject = malwareObjectList.get(position);
        holder.name.setText(holder.malwareObject.getName());
        holder.name.setId(position);
        holder.alt.setText(holder.malwareObject.getType());
        holder.alt.setId(position);
        holder.alt2.setText(holder.malwareObject.getPlatform() + ", " + holder.malwareObject.getFileType());
        holder.alt2.setId(position);
    }

    @Override
    public int getItemCount() {
        return malwareObjectList.size();
    }

}
